package com.zyh.scs.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyh.scs.admin.api.constant.RechargeTypeEnum;
import com.zyh.scs.admin.api.dto.TransactionPageDTO;
import com.zyh.scs.admin.api.entity.UserTransaction;
import com.zyh.scs.admin.api.utils.UserTypeToNameUtil;
import com.zyh.scs.admin.api.vo.UserTransactionExcelVO;
import com.zyh.scs.admin.mapper.UserTransactionMapper;
import com.zyh.scs.admin.service.UserTransactionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hwh
 * @ClassName: 统计报表交易流水  接口实现类$
 * @Description: $
 * @Date $
 * @Version 1.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserTransactionServiceImpl extends ServiceImpl<UserTransactionMapper, UserTransaction> implements UserTransactionService {

    private final UserTransactionMapper userTransactionMapper;

    @Override
    public IPage getTransactionPage(Page page, TransactionPageDTO transactionPageDTO) {
        Page<UserTransaction> userTransactionPage = new Page<>(page.getCurrent(), page.getSize());
        LambdaQueryWrapper<UserTransaction> queryWrapper = getUserTransactionLambdaQueryWrapper(transactionPageDTO, 2);
        return this.page(userTransactionPage, queryWrapper);
    }

    private static @NotNull LambdaQueryWrapper<UserTransaction> getUserTransactionLambdaQueryWrapper(TransactionPageDTO transactionPageDTO, Integer isExcel) {
        if (StrUtil.isBlank(transactionPageDTO.getTransactionTimeBegin()) && StrUtil.isBlank(transactionPageDTO.getTransactionTimeEnd())
                && StrUtil.isBlank(transactionPageDTO.getTransactionType()) && StrUtil.isBlank(transactionPageDTO.getAccountType())
                && StrUtil.isBlank(transactionPageDTO.getRechargeStream()) && StrUtil.isBlank(transactionPageDTO.getThirdRechargeStream())
                && StrUtil.isBlank(transactionPageDTO.getOrderId())
                && StrUtil.isBlank(transactionPageDTO.getOutOrderNo())) {
            transactionPageDTO.setTransactionTimeBegin(LocalDate.now().plusDays(-3).toString());
        }
        LambdaQueryWrapper<UserTransaction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(transactionPageDTO.getRechargeStream()), UserTransaction::getRechargeStream, transactionPageDTO.getRechargeStream())
                .eq(StrUtil.isNotBlank(transactionPageDTO.getThirdRechargeStream()), UserTransaction::getThirdRechargeStream, transactionPageDTO.getThirdRechargeStream())
                .eq(StrUtil.isNotBlank(transactionPageDTO.getAccountType()), UserTransaction::getAccountType, transactionPageDTO.getAccountType())
                .eq(StrUtil.isNotBlank(transactionPageDTO.getOutOrderNo()), UserTransaction::getOutOrderNo, transactionPageDTO.getOutOrderNo())
                .eq(StrUtil.isNotBlank(transactionPageDTO.getOrderId()), UserTransaction::getOrderId, transactionPageDTO.getOrderId())
                .eq(StrUtil.isNotBlank(transactionPageDTO.getTransactionType()), UserTransaction::getTransactionType, transactionPageDTO.getTransactionType())
                .ge(StrUtil.isNotBlank(transactionPageDTO.getTransactionTimeBegin()), UserTransaction::getTransactionTime, UserTypeToNameUtil.stringToLocalBeginTime(transactionPageDTO.getTransactionTimeBegin()))
                .le(StrUtil.isNotBlank(transactionPageDTO.getTransactionTimeEnd()), UserTransaction::getTransactionTime, UserTypeToNameUtil.stringToLocalEndTime(transactionPageDTO.getTransactionTimeEnd()))
                .orderByDesc(UserTransaction::getTransactionTime, UserTransaction::getTransactionId);

        if (isExcel == 1) {
            queryWrapper.last(" limit 100000");
        }
        return queryWrapper;
    }

    @Override
    public List<UserTransactionExcelVO> getExcel(TransactionPageDTO transactionPageDTO) {
        LambdaQueryWrapper<UserTransaction> queryWrapper = getUserTransactionLambdaQueryWrapper(transactionPageDTO, 1);
        if (transactionPageDTO.getBillType() != null) {
            if (transactionPageDTO.getBillType() == 2) {
                //餐补
                queryWrapper.eq(UserTransaction::getAccountType, "1");
            }
            if (transactionPageDTO.getBillType() == 3) {
                queryWrapper.eq(UserTransaction::getAccountType, "2");
            }
        }
        List<UserTransaction> userTransactionList = userTransactionMapper.selectList(queryWrapper);
        List<UserTransactionExcelVO> userTransactionExcelVOList = new ArrayList<>();
        AtomicInteger index = new AtomicInteger(1);
        userTransactionList.forEach(a -> {
            UserTransactionExcelVO userTransactionExcelVO = new UserTransactionExcelVO();
            BeanUtil.copyProperties(a, userTransactionExcelVO);
            userTransactionExcelVO.setIndex(index.getAndIncrement());
            if (a.getTransactionType() != null) {
                userTransactionExcelVO.setTransactionType(UserTypeToNameUtil.getTransactionType(a.getTransactionType()));
            }
            if (a.getAccountType().equals("1")) {
                userTransactionExcelVO.setAccountType(RechargeTypeEnum.MEAL_ACCOUNT.getDesc());
            } else {
                userTransactionExcelVO.setAccountType(RechargeTypeEnum.CASH_ACCOUNT.getDesc());
            }
            userTransactionExcelVOList.add(userTransactionExcelVO);
        });
        return userTransactionExcelVOList;
    }
}
